%Question 4
%*************************************************************
%Compute the cross-validation perplexity
%*************************************************************
testsetFlag = true;

load('imdata.mat', 'x','y')
%Use a subset of the data to perform linear regression
x = double(x);
y = double(y);

%Create a matrix of the three pixels
%Using a column of ones and the 3 attributes to form the phi vector
dataVector = [x(:,end), x(:,end-34), x(:, end-35)];

% if ~testsetFlag
% %Perform four-fold cross-validation
% averagePerplexity = cvProcedure(dataVector, y);
% else
% 
% %The test data
% xtrain = dataVector;
% ytrain = y;
% 
% phi = [ones(size(xtrain,1),1),xtrain];
% %phi = [xtrain];
% %Learn the parameters for linear regression
% inversephi = phi'*phi;
% 
% wEstimate = inversephi\(phi'*ytrain);
% 
% %****************************************************
% %Training completed
% %****************************************************
% load('imtestdata.mat');
% x = double(x);
% %Test on the test data
% dataVector = [x(:,end), x(:,end-34), x(:, end-35)];
% xtest = [ones(size(x,1),1) dataVector];
% Nt = size(xtest,1);
% xtrainVector = [ones(size(xtrain,1),1) xtrain];
% %The standard deviation of the data is needed
% InverseVariance = (1/size(xtrainVector,1))*sum((ytrain - xtrainVector*wEstimate).^2);
% variance = InverseVariance;
% 
% %P(yn|xn)=(1/sqrt(2pi?^2))*exp( (yn-wxn-b)^2/(2*?^2))
% predictionMatrix = zeros(size(xtest,1),64);
% %Store predictions in a matrix
% for ii=1:size(xtest,1)
%     for jj = 1:64
%     LogPyx = -1*(-0.5*log(2*pi*variance)-(((jj-1) - xtest(ii,:)*wEstimate)^2)/(2*variance));
%     %LogPyx = -log(normpdf(jj-1, xtest(ii,:)*wEstimate, sqrt(variance)));
%     predictionMatrix(ii,jj) = LogPyx;
%     end
%     %Find the minimum value in the row
%     minVal = min(predictionMatrix(ii,:));
%     predictionMatrix(ii,:) = predictionMatrix(ii,:) - minVal.*ones(1, size(predictionMatrix(ii,:),2));
%     ii
% end
% end
% csvwrite('predictionsLinearRegression1.csv',predictionMatrix);
%*************************************************************
%Question 4 (b)
%*************************************************************
load('PCA.mat');
%Get the first 10 components of PCA representation for the data
dataVectorEvecs = evecs(:,1:10);
%Compute the mean patch
meanPatch = mean(x);
%Subtract the mean from each of the data points
xNormalised = x - repmat(meanPatch,size(x,1),1);
%Project the data onto the first 10 principal components
xProjected = xNormalised*dataVectorEvecs;

%Perform 4-fold cross-validation
averagePerplexity = cvProcedure(xProjected, y);
%**************************************************************